package pers.zh.parking.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import pers.zh.parking.R;
import pers.zh.parking.data.UserSettings;
import pers.zh.parking.data.entity.User;
import pers.zh.parking.di.component.DaggerUserComponent;
import pers.zh.parking.mvp.contract.UserContract;
import pers.zh.parking.mvp.presenter.UserPresenter;
import pers.zjc.commonlibs.util.StringUtils;

import static com.jess.arms.utils.Preconditions.checkNotNull;

public class UserActivity extends BaseActivity<UserPresenter> implements UserContract.View {

    @BindView(R.id.ivLeft)
    ImageView ivLeft;
    @BindView(R.id.toolbar_left)
    RelativeLayout toolbarLeft;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.ivRight)
    ImageView ivRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.lbl_acoount)
    TextView lblAcoount;
    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.lbl_device_no)
    TextView lblDeviceNo;
    @BindView(R.id.et_user_name)
    EditText etUserName;
    @BindView(R.id.lbl_sex)
    TextView lblSex;
    @BindView(R.id.tv_desc)
    EditText tvDesc;
    @BindView(R.id.tvConfirm)
    TextView tvConfirm;
    @BindView(R.id.lbl_role)
    TextView lblRole;
    @BindView(R.id.et_role)
    TextView etRole;

    private UserSettings settings = UserSettings.getInstance();

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerUserComponent //如找不到该类,请编译一下项目
                            .builder().appComponent(appComponent).view(this).build().inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_user; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        initView();
        initData();
    }

    private void initData() {
        etAccount.setText(settings.getAccount());
        etUserName.setText(settings.getUserName());
        etRole.setText("用户");
        tvDesc.setText(settings.getDescription());
    }

    private void initView() {
        ivLeft.setOnClickListener(v -> killMyself());
        ivRight.setImageResource(R.drawable.ic_pwd);
        tvConfirm.setOnClickListener(v -> {
            String userName = etUserName.getText().toString();
            String desc = tvDesc.getText().toString();
            if (StringUtils.isEmpty(userName)) {
                showMessage("用户名不能为空");
                return;
            }

            User user = new User();
            user.setUserId(Integer.valueOf(settings.getUserId()));
            user.setUserName(userName);
            user.setDescription(desc);
            mPresenter.updateUser(user);
        });
        ivRight.setOnClickListener(v -> {
            launchActivity(new Intent(this, ModifyPwdActivity.class));
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void success() {
        showMessage("修改成功");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRelogin(String s) {
        finish();
    }
}
